
//
//  LZSetTableCell.m
//  彩票
//
//  Created by Macx on 16/6/4.
//  Copyright © 2016年 LiuZhuo. All rights reserved.
//

#import "LZSetTableCell.h"
#import "LZSwitch.h"
#import "LZLabel.h"
#import "LZAccess.h"

@class LZSetTableCell;
@interface LZSetTableCell ()
@property(nonatomic,strong)UIImageView * accessView;
@property(nonatomic,strong)UISwitch * switchView;
@property(nonatomic,strong)UILabel * labelView;

@end
@implementation LZSetTableCell
+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString * identifair = @"haha";

    LZSetTableCell * cell = [tableView dequeueReusableCellWithIdentifier:identifair];
    if (cell==nil) {
        cell=[[LZSetTableCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifair];
    }
    return cell;
}

-(void)setItem:(LZItem *)item{
    _item=item;
    if (_item.icon) {
         self.imageView.image = [UIImage imageNamed:item.icon];
    }
   
    self.textLabel.text = item.title;
    
    self.detailTextLabel.text=item.deLabel;
    self.detailTextLabel.font = [UIFont systemFontOfSize:13];
    [self setRightAccess];
    
   

}
-(void)setRightAccess{
    
   if ([_item isKindOfClass:[LZSwitch class]]) {
         LZSwitch * item = (LZSwitch *)_item;
        
       NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
       NSString *  ison1 = [userDefaults objectForKey:_item.title];

       if (ison1 == nil) {
            self.switchView.on=item.ison;
       }else if ([ison1 isEqualToString:@"开"]){
           self.switchView.on = YES;
       }else{
           self.switchView.on = NO;
       }
       
        self.accessoryView=self.switchView;
        
        
        
        
    }else if ([_item isKindOfClass:[LZLabel class]]){
        LZLabel * item = (LZLabel *)_item;
        self.labelView.text = item.label;
        [self.labelView sizeToFit];
        
        self.accessoryView=self.labelView;
    }else if ([_item isKindOfClass:[LZAccess class]]){
        self.accessoryView=self.accessView;
    }else{
        self.accessoryView=nil;
    }
    
}
-(UIImageView *)accessView{
    if (_accessView==nil) {
        _accessView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_icon_arrow"]];
    }
    return _accessView;
}
-(UILabel *)labelView{
    if (_labelView==nil) {
        _labelView=[[UILabel alloc]init];
    }
    return _labelView;
}
-(UISwitch *)switchView{
    if (_switchView==nil) {
       _switchView=[[UISwitch alloc]init];
        
        [_switchView addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
        
        
    }
    return _switchView;
}

-(void)valueChange:(UISwitch *)swi{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    if (swi.on) {
        [userDefaults setObject:@"开" forKey:_item.title];
    }else{
        
        [userDefaults setObject:@"关" forKey:_item.title];
    }
    
    [userDefaults synchronize];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.detailTextLabel.x = CGRectGetMaxX(self.textLabel.frame) + 5;
    
    
}











@end
